package server_gs

import (
	"encoding/json"

	"public.com/Framework/managecenterMgr"
	"public.com/goutil/debugUtil"
	"public.com/goutil/logUtil"
	"public.com/goutil/zlibUtil"

	. "7qule.com/sd3/CrossServer/src/bll/player/bll"
	. "7qule.com/sd3/CrossServer/src/bll/player/model"
)

// 处理客户端请求
// clientObj：对应的客户端对象
// message：请求内容字节数组(json格式)
// 返回值：无
func handleRequest(clientObj *client, message []byte) {
	// 先进行解压缩，再进行处理
	content, err := zlibUtil.Decompress(message)
	if err != nil {
		logUtil.ErrorLog("server_gs zlib解压缩错误，错误信息为：%s", err)
		return
	}

	// 更新信息
	increaseSize(len(content))

	// 将接收到的数据反序列化为GlobalPlayer对象
	gsPushMessageObj := new(GlobalPlayer)
	if err := json.Unmarshal(content, gsPushMessageObj); err != nil {
		logUtil.ErrorLog("server_gs 反序列化%s出错，错误信息为：%s", string(content), err)
		return
	}

	debugUtil.Printf("收到来自GS的数据：%v\n", gsPushMessageObj)

	// 处理数据
	handleMessage(gsPushMessageObj)
}

// 处理GameServer推送的消息
func handleMessage(gsPushMessageObj *GlobalPlayer) {
	debugUtil.Printf("Receive data from gs. %v\n", gsPushMessageObj)
	serverName := gsPushMessageObj.ServerName
	groupId := gsPushMessageObj.ServerGroupId
	_, serverObj, exists := managecenterMgr.GetServerGroup(gsPushMessageObj.PartnerId, gsPushMessageObj.ServerId)
	if exists == true {
		serverName = serverObj.Name
		groupId = serverObj.GroupId
	}

	// 更新玩家信息数据
	GlobalPlayerBll.UpdateInfo(gsPushMessageObj.Id, groupId, gsPushMessageObj.PartnerId, gsPushMessageObj.ServerId, serverName, gsPushMessageObj.Name, gsPushMessageObj.GuildId, gsPushMessageObj.GuildName, gsPushMessageObj.Lv, gsPushMessageObj.Vip, gsPushMessageObj.Fap, gsPushMessageObj.MaxFap, gsPushMessageObj.HeadImageId, gsPushMessageObj.FashionModelId, gsPushMessageObj.FashionLevel, gsPushMessageObj.Flag, gsPushMessageObj.Logo, gsPushMessageObj.BaseInfo, gsPushMessageObj.ClientPlayerStr)
}
